package chuishop.yiqimaicai.controller;
import chuishop.yiqimaicai.pojo.Goods;
import chuishop.yiqimaicai.pojo.Supplier;
import chuishop.yiqimaicai.service.GoodsService;
import chuishop.yiqimaicai.service.SupplierService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
//供应商表
public class SupplierController {
    @Autowired
    private SupplierService supplierService;
    //查询所有供应商信息
    @RequestMapping("findSupplier")
    @ResponseBody
    public PageInfo<Supplier> findSupplier(Integer pageNo,Integer pageSize){
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        //启动分页
        PageHelper.startPage(pageNo, pageSize);
        List<Supplier> list=supplierService.findSupplier();
        PageInfo<Supplier> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
    //根据输入查询供应商
    @RequestMapping("selectSupplier")
    @ResponseBody
    public PageInfo<Supplier> selectSupplier(Integer pageNo, Integer pageSize, String selectInput){
        if(pageNo==null){
            pageNo=1;
        }

        if(pageSize==null){
            pageSize = 5;
        }

        //启动分页
        PageHelper.startPage(pageNo,pageSize);

        List<Supplier> list = supplierService.selectSupplier(selectInput);
        PageInfo<Supplier> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    //添加供应商信息
    @RequestMapping("addSupplier")
    @ResponseBody
    public Map<Object,Object> addSupplier(@RequestBody Supplier supplier){

        Map<Object,Object> result=new HashMap<>();
        boolean flag=supplierService.addSupplier(supplier);
        if(flag){
            result.put("status","200");
            result.put("msg","添加成功");
        }else
        {
            result.put("status","500");
            result.put("msg","添加失败");
        }

        return result;



    }
    @RequestMapping("deleteSupplier")
    @ResponseBody
    public Map<Object,Object> deleteSupplier(String ids){

        String idstr[]=ids.split(",");
        Map<Object,Object> result=new HashMap<>();
         boolean flag= supplierService.deleteSupplier(idstr);
        if(flag){
            result.put("status","200");
            result.put("msg","删除成功");
        }else
        {
            result.put("status","500");
            result.put("msg","删除失败");
        }

        return result;
    }
    @RequestMapping("updateSupplier")
    @ResponseBody
    public Map<Object,Object> updateSupplier(@RequestBody Supplier supplier){


        Map<Object,Object> result=new HashMap<>();
        boolean flag= supplierService.updateSupplier(supplier);
        if(flag){
            result.put("status","200");
            result.put("msg","更新成功");
        }else
        {
            result.put("status","500");
            result.put("msg","更新失败");
        }

        return result;
    }

}
